<?php
/**
 * Created by 绵阳市碳素云信息技术有限责任公司.
 * User: castle
 * Date: 2015/06/26
 * Time: 8:58
 */

	function listen($listener,$serv,$fd,$data=null){
		//加载监听配置文件
		$config = include $listener.'.php';

	}

	function isWS($fd){
		$WS = S('WebSocket');
		return is_array($WS)&&array_search($fd,$WS)?true:false;
	}

	function http($url, $param, $data = '', $method = 'GET'){
		$opts = array(
			CURLOPT_TIMEOUT        => 30,
			CURLOPT_RETURNTRANSFER => 1,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_SSL_VERIFYHOST => false,
		);

		/* 根据请求类型设置特定参数 */
		$opts[CURLOPT_URL] = $url . '?' . http_build_query($param);

		if(strtoupper($method) == 'POST'){
			$opts[CURLOPT_POST] = 1;
			$opts[CURLOPT_POSTFIELDS] = $data;

			if(is_string($data)){ //发送JSON数据
				$opts[CURLOPT_HTTPHEADER] = array(
					'Content-Type: application/json; charset=utf-8',
					'Content-Length: ' . strlen($data),
				);
			}
		}

		/* 初始化并执行curl请求 */
		$ch = curl_init();
		curl_setopt_array($ch, $opts);
		$data  = curl_exec($ch);
		$error = curl_error($ch);
		curl_close($ch);

		//发生错误，抛出异常
		if($error) {
			return '';
		}

		return  $data;
	}
